<?php
/**
 * Created by Vscode.
 * User: xiayu 
 * Date: 2022/11/24
 * Time: 18:02
 */

namespace app\service;




class HttpService
{
    /**
     * @Description: 自定义的CURL请求
     * @author: xiayu <cb1071403016@126.com>
     * @param [String] $url      请求的路径
     * @param [String] $data     需要的参数
     * @Date: 2022-09-27 17:04:16
     * @version: 1.0.0
     * @return [Json]
     */
    public static function senHTTP($url, $data, $type = "http")
    {
        $curl = curl_init();

        if ($type == "json") {
            $headers = array("Content-type: application/json;charset=UTF-8");
            $data = json_encode($data);
            curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        }
        // if ($type == "EMQX") {
        //     curl_setopt($curl, CURLOPT_HTTPHEADER, ['Authorization: Basic ' . base64_encode($app_id . ':' . $app_key)]);
        // }
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);

        if (!empty($data)) {
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }

        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }
}